//
//  NSDate+Tripvi.m
//  Tripvi
//
//  Created by xiaowuxiaowu on 2017/5/4.
//  Copyright © 2017年 xiaowuxiaowu. All rights reserved.
//

#import "NSDate+Tripvi.h"

@implementation NSDate (Tripvi)

+(NSDate *)dateFromStringWithYYYYMMDD:(NSString *)string{
  
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:kNSDateHelperFormatSQLDateWithYYYYMMDD];
    return [formatter dateFromString:string];
    
}
+(NSDate *)dateFromStringWithYYYYMMDDSS:(NSString *)string{
    
    if(!string)
        return [NSDate date];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:kNSDateHelperFormatSQLDateWithTimeYYYYMMDDHHmmss];
    return [formatter dateFromString:string];
    
}
-(NSString *)stringFromDateWithFormat:(NSString *)format{

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:format];
    return [formatter stringFromDate:self];
}

+(NSString *)getTimeIntervalSince{

    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] * 1000;
    return [NSString stringWithFormat:@"%0.f",interval];
}

+(NSString *)timeWithTimeIntervalString:(long long)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:kNSDateHelperFormatSQLDateWithYYYYMMDD];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:timeString];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}
@end
